package com.copro.chemdb.industry;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.db.sql.SQLSegment;

import com.copro.simple.Application;
import com.copro.simple.SimpleException;
import com.copro.simple.db.QueryCallback;

public class IndustryUtils {

	static class SubIndustryListGetter implements QueryCallback {

		private List<SubIndustry> list;

		public SubIndustryListGetter(List<SubIndustry> list) {
			this.list = list;
		}

		public List<SubIndustry> getList() {
			return this.list;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			while (rs.next()) {
				list.add(new SubIndustry(rs));
			}
		}
	}

	static class MainIndustryListGetter implements QueryCallback {

		private List<MainIndustry> list;

		public MainIndustryListGetter(List<MainIndustry> list) {
			this.list = list;
		}

		public List<MainIndustry> getList() {
			return this.list;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			while (rs.next()) {
				list.add(new MainIndustry(rs));
			}
		}
	}

	public static List<MainIndustry> getAllMainIndustry(Application app,
			String type) throws SimpleException {
		MainIndustryListGetter callback = new MainIndustryListGetter(
				new LinkedList<MainIndustry>());
		SQLSegment sql = null;
		if ("GB".equals(type)) {
			sql = app.getSQL("allMainGBIndustry");
		} else {
			sql = app.getSQL("allMainIndustry");
		}
		app.query(sql.toString(), callback);
		return callback.getList();
	}

	public static List<SubIndustry> getAllSubIndustry(Application app,
			String type) throws SimpleException {
		SubIndustryListGetter callback = new SubIndustryListGetter(
				new LinkedList<SubIndustry>());
		SQLSegment sql = null;
		if ("GB".equals(type)) {
			sql = app.getSQL("allSubGBIndustry");
		} else {
			sql = app.getSQL("allSubIndustry");
		}
		app.query(sql.toString(), callback);
		return callback.getList();
	}

}
